package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {

    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String EXPRESSIONS_IN_SESSION = "EXPRESSIONS_IN_SESSION";

    // 获取请求对象
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
        return requestAttributes.getRequest();
    }

    // 获取 session 对象
    public static HttpSession getSession() {

        return getRequest().getSession();
    }

    // 往 session 存到登录员工对象
    public static void setEmployee(Employee employee) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION, employee);
    }

    // 从 session 中取员工对象
    public static Employee getEmployee() {
        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    // 往 session 存到权限表达式
    public static void setExpressions(List<String> expressions) {
        getSession().setAttribute(EXPRESSIONS_IN_SESSION, expressions);
    }

    // 从 session 中取权限表达式
    public static List<String> getExpressions() {
        return (List<String>) getSession().getAttribute(EXPRESSIONS_IN_SESSION);
    }
}
